<?php
namespace app\spadmin\command;


use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\Db;
use dingtalk\DingtalkReboot;

class DingReboot extends Command
{

    protected function configure()
    {
        $this->setName('dingReboot')->setDescription('dingReboot send order ');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln('dingtalk department user update start');
        $this->firstOrder();
        
        if(date('N')==1 && date('Hi') == '0930'){
        
            $this->oneOrder();
        }
        if(date('N')==5 && date('Hi') == '0930'){
        
            $this->otherOrder();
        }
        $output->writeln('end');
    }
    
    protected function firstOrder(){
        $row = Db::name('ProductOrder')->where('status',1)->where('is_call',0)->find();
        if(!empty($row)){

            $product = Db::name('product')->where('id',$row['product_id'])->find();
            $cate = Db::name('ProductCategory')->column('id,title','id');
            $user = Db::name('User')->where('id',$row['user_id'])->find();
            $level = [
                '0'=>'登录用户',
                '1'=>'注册会员',
                '2'=>'合伙人',
                '3'=>'高级合伙人',
                '4'=>'资深合伙人'
            ];
            $text = '**'.$level[$row['user_level_id']].'--'.$user['nickname'].'['.$user['mobile'].']'.'提交了'.$cate[$product['cate_id']].'-'. $product['title'].'业务订单**    \n';
            $text .= "详情如下：     \n";
            $text.= "订单编号：**{$row['order_sn']}**     \n";
            $text.= "业务名称：**{$cate[$product['cate_id']]}-{$product['title']}**     \n";
            $text.= "客户姓名：**{$row['contacts']}**       \n";
            $text.= "联系方式：**{$row['contacts_phone']}**     \n";
            $text.= "公司名称：{$row['company_name']}       \n";
            $text.= "业务描述：     \n".htmlspecialchars($row['remark'])."      \n";
            if($row['s_id']>0){
                $suser = Db::name('User')->where('id',$row['s_id'])->find();
                $row['suser']=$suser['nickname'].'['.$suser['mobile'].']';
                $row['suser_level']=$level[$row['s_level_id']];
                $text.= "受益合伙人：".$row['suser']."--".$row['suser_level']."     \n";
           
            }
            if($row['p_id']>0){
                $puser = Db::name('User')->where('id',$row['p_id'])->find();
                $row['puser']=$puser['nickname'].'['.$puser['mobile'].']';
                $row['puser_level']=$level[$row['p_level_id']];
                $text.= "上级合伙人：".$row['puser']."--".$row['suser_level']."     \n";
            }
            if($row['t_id']>0){
                $tuser = Db::name('User')->where('id',$row['t_id'])->find();
                $row['tuser']=$tuser['nickname'].'['.$tuser['mobile'].']';
                $row['tuser_level']=$level[$row['t_level_id']];
                $text.= "顶级合伙人：".$row['tuser']."--".$row['tuser_level']."     \n";
            }
            $content = "{\"title\": \"新的业务订单提醒\",\"text\": \"".$text."\"}";

            DingtalkReboot::instance()->rebootTxt($content);
            Db::name('ProductOrder')->where('id',$row['id'])->update(['is_call'=>1]);
        }
    }
    
    protected function otherOrder(){
        $order = Db::name('ProductOrder')->where('status','5')->select();
        if(!empty($order)){
            $text = '';
            foreach ($order as $key => $row) {
                $product = Db::name('product')->where('id',$row['product_id'])->find();
                $cate = Db::name('ProductCategory')->column('id,title','id');
                $text.= "订单编号：**{$row['order_sn']}**     \n";
                $text.= "业务名称：**{$cate[$product['cate_id']]}-{$product['title']}**     \n";
                $text.= "客户姓名：**{$row['contacts']}**       \n";
                $text.= "联系方式：**{$row['contacts_phone']}**     \n";
                $text.= "公司名称：**{$row['company_name']}**       \n";
                if($product['module_id'] == 3){
                    $store = Db::name('Store')->where('id',$row['store_id'])->value('nickname');
                    $store_type="经纪人";
                }else{
                    $store = Db::name('Store')->where('id',$row['store_id'])->value('company_name');
                    $store_type="供应商";
                }
                $text.= $store_type."：**{$store}**       \n";
                $text.= "渠道经营费：**{$row['corporate_income']}**       \n";
                $text.="—————————————————————       \n";
            }
            $text = "完结订单跟踪提醒：   \n" . $text." 请提交收款和业务完结单";
            $content = "{\"title\": \"完结订单跟踪提醒\",\"text\": \"".$text."\"}";
            dump($content);
            DingtalkReboot::instance()->rebootTxt($content);
        }
    }
    
    protected function oneOrder(){
        $count1 = Db::name('ProductOrder')->where('status',1)->count('id');
        $count2 = Db::name('ProductOrder')->where('status','in','2,3')->count('id');
        $count3 = Db::name('ProductOrder')->where('status','4')->count('id');
        $count4 = Db::name('ProductOrder')->where('status',5)->count('id');
        
        $text = '';
        if(!empty($count1)){
            $text .= "**".$count1 . "**笔业务还未分配服务商,请及时分配   \n";
        }
        if(!empty($count2)){
            $text .="**".$count2 . "**笔业务在途中,请及时跟进   \n";
        }
        if(!empty($count3)){
            $text .="**".$count3 . "**笔业务待结算,请联系服务商打款结算   \n";
        }
        if(!empty($count4)){
            $text .="**".$count4 . "**笔业务已结算,请尽快提交业务完结单   \n";
        }
        if(!empty($text)){
            $text = "每周业务订单汇总   \n" . $text;
            $content = "{\"title\": \"每周业务订单汇总\",\"text\": \"".$text."\"}";
        DingtalkReboot::instance()->rebootTxt($content);
            
        }
    }
}